package com.funtl.itoken.service.sso.controller;

import com.funtl.itoken.common.domain.TbSysUser;
import com.funtl.itoken.common.utils.CookieUtils;
import com.funtl.itoken.common.utils.MapperUtils;
import com.funtl.itoken.service.sso.service.LoginService;
import com.funtl.itoken.service.sso.service.consumer.RedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public String login(String loginCode,
                        String plantPassword,
                        HttpServletRequest httpServletRequest,
                        HttpServletResponse httpServletResponse,
                        @RequestParam(required = false) String url,
                        RedirectAttributes redirectAttributes) {
        TbSysUser tbSysUser = loginService.login(loginCode, plantPassword);
        if (tbSysUser != null) {
            String token = UUID.randomUUID().toString();
            String put = redisService.put(token, loginCode, 60 * 60 * 24);
            if (StringUtils.equals(put, "ok")) {
                CookieUtils.setCookie(httpServletRequest, httpServletResponse, "token", token);
                return "redirect:" + url;
            } else {
                // 熔断
                redirectAttributes.addAttribute("message", "服务器异常，请稍后重试");
            }
        } else {
            redirectAttributes.addAttribute("message", "用户名或密码错误");

        }

        return "redirect:/login";

        // return null;
    }

    @GetMapping("/login")
    public String login(HttpServletRequest httpServletRequest,
                        @RequestParam(required = false) String url,
                        Model model) {
        String token = CookieUtils.getCookieValue(httpServletRequest, "token");
        if(!StringUtils.isBlank(token)){
            // cookie有token

            // 判断是否已登录
            String loginCode = redisService.get(token);
            if(!StringUtils.isBlank(loginCode)){
                String json = redisService.get(loginCode);
                try {
                    TbSysUser tbSysUser = MapperUtils.json2pojo(json, TbSysUser.class);
                    if(tbSysUser!=null){
                        // 已登录
                        if(!StringUtils.isBlank(url)){
                            model.addAttribute("tbSysUser", tbSysUser);
                            return "redirect:"+url;
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if(StringUtils.isNotBlank(url)){
            model.addAttribute("url",url);
        }


        return "login";


    }

    @GetMapping("logout")
    public String  logout(HttpServletRequest httpServletRequest,
                        HttpServletResponse httpServletResponse,
                          Model model,
                          @RequestParam(required = false) String url){
        try {
            CookieUtils.deleteCookie(httpServletRequest,httpServletResponse , "token");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return login(httpServletRequest, url, model);
    }

}
